package cn.lhn.ridersclub.service;

import cn.lhn.ridersclub.dto.AccountCardDto;
import cn.lhn.ridersclub.entity.Account;
import cn.lhn.ridersclub.entity.Club;
import cn.lhn.ridersclub.entity.ClubFollow;
import cn.lhn.ridersclub.mapper.AccountMapper;
import cn.lhn.ridersclub.mapper.ClubFollowMapper;
import cn.lhn.ridersclub.mapper.ClubMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClubFollowServiceImpl implements ClubFollowService {

    private final ClubFollowMapper clubFollowMapper;

    private final ClubMapper clubMapper;

    private final AccountMapper accountMapper;

    public ClubFollowServiceImpl(ClubFollowMapper clubFollowMapper, ClubMapper clubMapper, AccountMapper accountMapper) {
        this.clubFollowMapper = clubFollowMapper;
        this.clubMapper = clubMapper;
        this.accountMapper = accountMapper;
    }

    @Override
    public void addFollow(Long clubId, Long accountId) {

        //  判断club是否存在
        Club club = clubMapper.selectById(clubId);
        if (club == null) {
            throw new RuntimeException("该车友圈不存在");
        }

        //  判断账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account == null) {
            throw new RuntimeException("账号不存在");
        }

        //  查看是否已经关注
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        qw.eq("account_id", accountId);
        ClubFollow follow = clubFollowMapper.selectOne(qw);
        if (follow != null) {
            throw new RuntimeException("请勿重复关注");
        }

        //  添加关注记录
        int i = clubFollowMapper.insert(new ClubFollow(0L, clubId, accountId));
        if (i == 0) {
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void deleteFollow(Long clubId, Long accountId) {
        //  删除关注记录
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        qw.eq("account_id", accountId);
        int i = clubFollowMapper.delete(qw);
        if (i == 0) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public Boolean isFollow(Long clubId, Long accountId) {
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        qw.eq("account_id", accountId);
        return clubFollowMapper.selectOne(qw) != null;
    }

    @Override
    public List<ClubFollow> getFollows(Long accountId) {
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", accountId);
        return clubFollowMapper.selectList(qw);
    }

    @Override
    public Long getFansCount(Long clubId) {
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        return clubFollowMapper.selectCount(qw);
    }

    @Override
    public List<AccountCardDto> listFans(Long clubId) {
        QueryWrapper<ClubFollow> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        List<ClubFollow> followList = clubFollowMapper.selectList(qw);
        List<AccountCardDto> list = new ArrayList<>();
        for (ClubFollow follow : followList) {
            Account account = accountMapper.selectById(follow.getAccountId());
            list.add(AccountCardDto
                    .builder()
                    .id(follow.getId())
                    .accountId(account.getId())
                    .profile(account.getProfile())
                    .nickname(account.getNickname())
                    .build());
        }
        return list;
    }
}
